********************************************************************************
**  TITLE:      on2011_ipsos_invitation                          	          **  
**  AUTHORS:    P. Mongrain, N. Fréchet, B. Thompson Collart, and Y. Dufresne **
**  DATA:       2011 Ontario General Election Invitation Survey               **
**	DATE:		February 2025	 					                          **	
**  VERSION:	Stata 17					                                  **	
********************************************************************************

* Note: The data file needs to be downloaded from its original source (see the "surveys.text" file)

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "on2011_ipsos_invitation", replace

* Open the dataset

import spss using "2011 Ontario General Election Invitation Survey.sav", clear case(lower)

* Election

gen election = "on2011"

* Type of survey

gen type = "invitation"

gen exit_survey = 0

* Respondent ID

gen responseid = respid

* Survey weight

merge 1:1 election responseid using on2011_invitation_weight
drop _merge


***********************
** DISTRICT OUTCOMES **
***********************

decode riding, generate(riding_str)

replace riding_str = subinstr(riding_str, "EASTERN - ", "", .)
replace riding_str = subinstr(riding_str, "NORTHERN - ", "", .)
replace riding_str = subinstr(riding_str, "CENTRAL - ", "", .)
replace riding_str = subinstr(riding_str, "SOUTHWEST - ", "", .)
replace riding_str = subinstr(riding_str, "GTA 905 - ", "", .)
replace riding_str = subinstr(riding_str, "GTA 416 - ", "", .)

gen str district_name = substr(riding_str,7,100)

gen str riding_code = substr(riding_str,1,3)
replace riding_code = "35" + riding_code 
replace riding_code = "" if riding_str == "Not specified"
destring riding_code, gen(district_code)

replace district_name = "LaSalle - Emard" if district_code == 24029

* Election outcome in each district

gen district_outcome = .

replace district_outcome = 352 if district_code == 35001
replace district_outcome = 353 if district_code == 35002
replace district_outcome = 352 if district_code == 35003
replace district_outcome = 351 if district_code == 35004
replace district_outcome = 353 if district_code == 35005
replace district_outcome = 353 if district_code == 35006
replace district_outcome = 352 if district_code == 35007
replace district_outcome = 352 if district_code == 35008
replace district_outcome = 352 if district_code == 35009
replace district_outcome = 351 if district_code == 35010
replace district_outcome = 351 if district_code == 35011
replace district_outcome = 351 if district_code == 35012
replace district_outcome = 351 if district_code == 35013
replace district_outcome = 351 if district_code == 35014
replace district_outcome = 353 if district_code == 35015
replace district_outcome = 352 if district_code == 35016
replace district_outcome = 352 if district_code == 35017
replace district_outcome = 351 if district_code == 35018
replace district_outcome = 351 if district_code == 35019
replace district_outcome = 352 if district_code == 35020
replace district_outcome = 351 if district_code == 35021
replace district_outcome = 353 if district_code == 35022
replace district_outcome = 352 if district_code == 35023
replace district_outcome = 352 if district_code == 35024
replace district_outcome = 352 if district_code == 35025
replace district_outcome = 352 if district_code == 35026
replace district_outcome = 352 if district_code == 35027
replace district_outcome = 351 if district_code == 35028
replace district_outcome = 351 if district_code == 35029
replace district_outcome = 351 if district_code == 35030
replace district_outcome = 353 if district_code == 35031
replace district_outcome = 353 if district_code == 35032
replace district_outcome = 353 if district_code == 35033
replace district_outcome = 351 if district_code == 35034
replace district_outcome = 353 if district_code == 35035
replace district_outcome = 352 if district_code == 35036
replace district_outcome = 352 if district_code == 35037
replace district_outcome = 351 if district_code == 35038
replace district_outcome = 351 if district_code == 35039
replace district_outcome = 351 if district_code == 35040
replace district_outcome = 351 if district_code == 35041
replace district_outcome = 351 if district_code == 35042
replace district_outcome = 353 if district_code == 35043
replace district_outcome = 352 if district_code == 35044
replace district_outcome = 352 if district_code == 35045
replace district_outcome = 352 if district_code == 35046
replace district_outcome = 352 if district_code == 35047
replace district_outcome = 352 if district_code == 35048
replace district_outcome = 352 if district_code == 35049
replace district_outcome = 352 if district_code == 35050
replace district_outcome = 352 if district_code == 35051
replace district_outcome = 351 if district_code == 35052
replace district_outcome = 351 if district_code == 35053
replace district_outcome = 352 if district_code == 35054
replace district_outcome = 351 if district_code == 35055
replace district_outcome = 353 if district_code == 35056
replace district_outcome = 351 if district_code == 35057
replace district_outcome = 351 if district_code == 35058
replace district_outcome = 352 if district_code == 35059
replace district_outcome = 352 if district_code == 35060
replace district_outcome = 351 if district_code == 35061
replace district_outcome = 352 if district_code == 35062
replace district_outcome = 352 if district_code == 35063
replace district_outcome = 352 if district_code == 35064
replace district_outcome = 352 if district_code == 35065
replace district_outcome = 352 if district_code == 35066
replace district_outcome = 351 if district_code == 35067
replace district_outcome = 353 if district_code == 35068
replace district_outcome = 351 if district_code == 35069
replace district_outcome = 351 if district_code == 35070
replace district_outcome = 352 if district_code == 35071
replace district_outcome = 352 if district_code == 35072
replace district_outcome = 351 if district_code == 35073
replace district_outcome = 351 if district_code == 35074
replace district_outcome = 352 if district_code == 35075
replace district_outcome = 352 if district_code == 35076
replace district_outcome = 352 if district_code == 35077
replace district_outcome = 351 if district_code == 35078
replace district_outcome = 352 if district_code == 35079
replace district_outcome = 352 if district_code == 35080
replace district_outcome = 352 if district_code == 35081
replace district_outcome = 352 if district_code == 35082
replace district_outcome = 352 if district_code == 35083
replace district_outcome = 352 if district_code == 35084
replace district_outcome = 351 if district_code == 35085
replace district_outcome = 351 if district_code == 35086
replace district_outcome = 351 if district_code == 35087
replace district_outcome = 352 if district_code == 35088
replace district_outcome = 351 if district_code == 35089
replace district_outcome = 352 if district_code == 35090
replace district_outcome = 352 if district_code == 35091
replace district_outcome = 353 if district_code == 35092
replace district_outcome = 353 if district_code == 35093
replace district_outcome = 352 if district_code == 35094
replace district_outcome = 353 if district_code == 35095
replace district_outcome = 353 if district_code == 35096
replace district_outcome = 352 if district_code == 35097
replace district_outcome = 353 if district_code == 35098
replace district_outcome = 351 if district_code == 35099
replace district_outcome = 351 if district_code == 35100
replace district_outcome = 352 if district_code == 35101
replace district_outcome = 352 if district_code == 35102
replace district_outcome = 352 if district_code == 35103
replace district_outcome = 352 if district_code == 35104
replace district_outcome = 351 if district_code == 35105
replace district_outcome = 352 if district_code == 35106
replace district_outcome = 352 if district_code == 35107

label define outcome 351 "Progressive Conservative Party of Ontario" 352 "Ontario Liberal Party" 353 "Ontario New Democratic Party" 354 "Green Party of Ontario" 88 "Other"
label values district_outcome outcome


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by district

gen margin = .

replace margin = 11.8016321406152 if district_code == 35001
replace margin = 16.0928373808792 if district_code == 35002
replace margin = 9.1153139696829 if district_code == 35003
replace margin = 5.84783112966829 if district_code == 35004
replace margin = 10.7404988899047 if district_code == 35005
replace margin = 5.22583310382814 if district_code == 35006
replace margin = 8.25084380406728 if district_code == 35007
replace margin = 10.9037104484407 if district_code == 35008
replace margin = 2.43162430744878 if district_code == 35009
replace margin = 21.0090543746671 if district_code == 35010
replace margin = 4.33486423334139 if district_code == 35011
replace margin = 4.61829354762468 if district_code == 35012
replace margin = 16.2052450727295 if district_code == 35013
replace margin = 9.64248217936675 if district_code == 35014
replace margin = 4.5206048786726 if district_code == 35015
replace margin = 23.8831615120275 if district_code == 35016
replace margin = 27.7142925304769 if district_code == 35017
replace margin = 20.1948137422667 if district_code == 35018
replace margin = 19.7212421380202 if district_code == 35019
replace margin = 20.6006679887277 if district_code == 35020
replace margin = 21.0500835128658 if district_code == 35021
replace margin = 2.98403280691039 if district_code == 35022
replace margin = 18.6990533016984 if district_code == 35023
replace margin = 21.782360522924 if district_code == 35024
replace margin = 24.1025229633789 if district_code == 35025
replace margin = 3.41531414915862 if district_code == 35026
replace margin = 16.8340578623895 if district_code == 35027
replace margin = 41.3892105771087 if district_code == 35028
replace margin = 11.8500752063092 if district_code == 35029
replace margin = 5.33731201573388 if district_code == 35030
replace margin = 43.8674889027914 if district_code == 35031
replace margin = 25.4130088294077 if district_code == 35032
replace margin = 12.7785246732638 if district_code == 35033
replace margin = 10.008043973723 if district_code == 35034
replace margin = 11.9737140267392 if district_code == 35035
replace margin = 25.0563285405682 if district_code == 35036
replace margin = 0.823181609664103 if district_code == 35037
replace margin = 8.68339096103387 if district_code == 35038
replace margin = 7.73348956544577 if district_code == 35039
replace margin = 16.4165014632304 if district_code == 35040
replace margin = 22.2452851244281 if district_code == 35041
replace margin = 46.1717544272673 if district_code == 35042
replace margin = 12.490869247626 if district_code == 35043
replace margin = 14.9798405571337 if district_code == 35044
replace margin = 16.1675921251893 if district_code == 35045
replace margin = 21.1053522034536 if district_code == 35046
replace margin = 15.6382978723404 if district_code == 35047
replace margin = 12.4782851927097 if district_code == 35048
replace margin = 9.32892228819315 if district_code == 35049
replace margin = 14.6245551158565 if district_code == 35050
replace margin = 22.0022734203887 if district_code == 35051
replace margin = 27.5105837890873 if district_code == 35052
replace margin = 11.6231890449624 if district_code == 35053
replace margin = 1.06428449307574 if district_code == 35054
replace margin = 24.9683065472539 if district_code == 35055
replace margin = 30.6384500357584 if district_code == 35056
replace margin = 21.5221215872809 if district_code == 35057
replace margin = 1.45956770370982 if district_code == 35058
replace margin = 7.64196879942934 if district_code == 35059
replace margin = 10.1448633322996 if district_code == 35060
replace margin = 6.07232203775301 if district_code == 35061
replace margin = 17.6812971431965 if district_code == 35062
replace margin = 6.06434065700565 if district_code == 35063
replace margin = 15.4271143221419 if district_code == 35064
replace margin = 28.06879348825 if district_code == 35065
replace margin = 2.27088584803745 if district_code == 35066
replace margin = 29.876753081173 if district_code == 35067
replace margin = 8.77440128798551 if district_code == 35068
replace margin = 35.878436725257 if district_code == 35069
replace margin = 0.567184334908843 if district_code == 35070
replace margin = 8.44054420650253 if district_code == 35071
replace margin = 13.2492437060965 if district_code == 35072
replace margin = 7.03323371458104 if district_code == 35073
replace margin = 54.7979992304733 if district_code == 35074
replace margin = 11.1275810058772 if district_code == 35075
replace margin = 3.99344185501815 if district_code == 35076
replace margin = 37.4758141594983 if district_code == 35077
replace margin = 22.8716049382716 if district_code == 35078
replace margin = 24.1249914716518 if district_code == 35079
replace margin = 14.7252954488308 if district_code == 35080
replace margin = 27.4977698483497 if district_code == 35081
replace margin = 20.2859472001003 if district_code == 35082
replace margin = 5.9054685353119 if district_code == 35083
replace margin = 12.6402031623182 if district_code == 35084
replace margin = 32.0225562297648 if district_code == 35085
replace margin = 32.7468660655377 if district_code == 35086
replace margin = 33.595922150139 if district_code == 35087
replace margin = 1.6380294290033 if district_code == 35088
replace margin = 5.78670705630791 if district_code == 35089
replace margin = 1.65407854984894 if district_code == 35090
replace margin = 10.1522454288119 if district_code == 35091
replace margin = 24.2055147788137 if district_code == 35092
replace margin = 12.7731092436975 if district_code == 35093
replace margin = 29.7479101358412 if district_code == 35094
replace margin = 23.4009906320663 if district_code == 35095
replace margin = 2.4282607768729 if district_code == 35096
replace margin = 21.7837826888407 if district_code == 35097
replace margin = 12.5311620886032 if district_code == 35098
replace margin = 28.7875201212006 if district_code == 35099
replace margin = 14.7674098541151 if district_code == 35100
replace margin = 17.2436920372812 if district_code == 35101
replace margin = 9.98630173779915 if district_code == 35102
replace margin = 10.4772208901105 if district_code == 35103
replace margin = 9.84102484951381 if district_code == 35104
replace margin = 28.2424994185596 if district_code == 35105
replace margin = 2.36873527608352 if district_code == 35106
replace margin = 15.6646685472496 if district_code == 35107

* Effective number of electoral parties by district

gen enep = .

replace enep = 2.69199630455734 if district_code == 35001
replace enep = 2.98144258189552 if district_code == 35002
replace enep = 2.93209676608835 if district_code == 35003
replace enep = 3.08246014669991 if district_code == 35004
replace enep = 2.70622260207851 if district_code == 35005
replace enep = 3.25998985592759 if district_code == 35006
replace enep = 2.83924372316572 if district_code == 35007
replace enep = 2.9707389574939 if district_code == 35008
replace enep = 3.15783587704476 if district_code == 35009
replace enep = 3.11089371583531 if district_code == 35010
replace enep = 3.03205234684088 if district_code == 35011
replace enep = 3.1958357134846 if district_code == 35012
replace enep = 2.61008808846147 if district_code == 35013
replace enep = 3.00603583698816 if district_code == 35014
replace enep = 2.56671360995013 if district_code == 35015
replace enep = 2.70297762987611 if district_code == 35016
replace enep = 2.26509299372499 if district_code == 35017
replace enep = 3.0720315903232 if district_code == 35018
replace enep = 2.78748028281907 if district_code == 35019
replace enep = 2.40582220879416 if district_code == 35020
replace enep = 2.84789621238481 if district_code == 35021
replace enep = 3.02692350313595 if district_code == 35022
replace enep = 2.58127720023594 if district_code == 35023
replace enep = 2.69906154698121 if district_code == 35024
replace enep = 2.92057676492443 if district_code == 35025
replace enep = 2.73813718923452 if district_code == 35026
replace enep = 3.25252521403078 if district_code == 35027
replace enep = 2.28753506179442 if district_code == 35028
replace enep = 2.8555504306369 if district_code == 35029
replace enep = 2.71234970767997 if district_code == 35030
replace enep = 2.34834874635374 if district_code == 35031
replace enep = 2.68786793725948 if district_code == 35032
replace enep = 2.89416988881858 if district_code == 35033
replace enep = 2.99280213431151 if district_code == 35034
replace enep = 2.51044806509614 if district_code == 35035
replace enep = 2.88652900289002 if district_code == 35036
replace enep = 2.96372254360398 if district_code == 35037
replace enep = 2.8340799803796 if district_code == 35038
replace enep = 2.85758840469462 if district_code == 35039
replace enep = 2.94358861633346 if district_code == 35040
replace enep = 2.75349796077605 if district_code == 35041
replace enep = 2.17733329643899 if district_code == 35042
replace enep = 3.15222631088525 if district_code == 35043
replace enep = 3.03743822450185 if district_code == 35044
replace enep = 2.91404134753003 if district_code == 35045
replace enep = 2.55399356193831 if district_code == 35046
replace enep = 3.01240619994221 if district_code == 35047
replace enep = 2.86615090370313 if district_code == 35048
replace enep = 2.78149577218964 if district_code == 35049
replace enep = 2.51246125904727 if district_code == 35050
replace enep = 2.64907477151359 if district_code == 35051
replace enep = 2.54911744635811 if district_code == 35052
replace enep = 2.69182348047739 if district_code == 35053
replace enep = 3.12930728319955 if district_code == 35054
replace enep = 2.7594533223847 if district_code == 35055
replace enep = 2.53963057946301 if district_code == 35056
replace enep = 2.7267555778309 if district_code == 35057
replace enep = 2.95984825019955 if district_code == 35058
replace enep = 2.80138769398199 if district_code == 35059
replace enep = 2.58880946702329 if district_code == 35060
replace enep = 2.93452133993124 if district_code == 35061
replace enep = 2.94510680068964 if district_code == 35062
replace enep = 2.56205394926933 if district_code == 35063
replace enep = 2.70652068678024 if district_code == 35064
replace enep = 2.77096392233681 if district_code == 35065
replace enep = 2.84789299114732 if district_code == 35066
replace enep = 2.56840966078515 if district_code == 35067
replace enep = 2.71422763304873 if district_code == 35068
replace enep = 2.72488806696937 if district_code == 35069
replace enep = 2.91745519874861 if district_code == 35070
replace enep = 3.07832824616264 if district_code == 35071
replace enep = 2.79852423754817 if district_code == 35072
replace enep = 3.00597471540278 if district_code == 35073
replace enep = 1.85577827096172 if district_code == 35074
replace enep = 2.73042795013483 if district_code == 35075
replace enep = 2.99187063258595 if district_code == 35076
replace enep = 2.42069879028441 if district_code == 35077
replace enep = 2.88521956467237 if district_code == 35078
replace enep = 2.43048135744509 if district_code == 35079
replace enep = 2.8685577934544 if district_code == 35080
replace enep = 2.70170803688578 if district_code == 35081
replace enep = 2.78095388910417 if district_code == 35082
replace enep = 2.93834737769571 if district_code == 35083
replace enep = 2.9451390955065 if district_code == 35084
replace enep = 2.67374459019225 if district_code == 35085
replace enep = 2.58910265709675 if district_code == 35086
replace enep = 2.53063694954672 if district_code == 35087
replace enep = 2.74275088649803 if district_code == 35088
replace enep = 2.5369274820973 if district_code == 35089
replace enep = 2.94517352534672 if district_code == 35090
replace enep = 2.81573075324203 if district_code == 35091
replace enep = 2.75025923131251 if district_code == 35092
replace enep = 2.53308578303412 if district_code == 35093
replace enep = 2.56120103591993 if district_code == 35094
replace enep = 2.52751413068295 if district_code == 35095
replace enep = 2.81736614998369 if district_code == 35096
replace enep = 2.55034403603901 if district_code == 35097
replace enep = 2.92072228632495 if district_code == 35098
replace enep = 2.48037071243946 if district_code == 35099
replace enep = 2.71732345137734 if district_code == 35100
replace enep = 2.57493726440802 if district_code == 35101
replace enep = 2.98229366399441 if district_code == 35102
replace enep = 3.04800027726171 if district_code == 35103
replace enep = 2.83402608543692 if district_code == 35104
replace enep = 2.70556099402188 if district_code == 35105
replace enep = 2.57066988034658 if district_code == 35106
replace enep = 2.55789608021229 if district_code == 35107

* Competition measure

gen competition = .

replace competition = .2838886 if district_code == 35001
replace competition = .3478785 if district_code == 35002
replace competition = .3042198 if district_code == 35003
replace competition = .3027219 if district_code == 35004
replace competition = .2791187 if district_code == 35005
replace competition = .320889 if district_code == 35006
replace competition = .2839053 if district_code == 35007
replace competition = .3207323 if district_code == 35008
replace competition = .282719 if district_code == 35009
replace competition = .4151651 if district_code == 35010
replace competition = .2836074 if district_code == 35011
replace competition = .3034222 if district_code == 35012
replace competition = .304548 if district_code == 35013
replace competition = .3095334 if district_code == 35014
replace competition = .2050026 if district_code == 35015
replace competition = .361981 if district_code == 35016
replace competition = .3141825 if district_code == 35017
replace competition = .4008859 if district_code == 35018
replace competition = .3525784 if district_code == 35019
replace competition = .2953983 if district_code == 35020
replace competition = .3603284 if district_code == 35021
replace competition = .2624959 if district_code == 35022
replace competition = .3164772 if district_code == 35023
replace competition = .3546419 if district_code == 35024
replace competition = .3917229 if district_code == 35025
replace competition = .2271493 if district_code == 35026
replace competition = .390877 if district_code == 35027
replace competition = .3784076 if district_code == 35028
replace competition = .3103381 if district_code == 35029
replace competition = .2386991 if district_code == 35030
replace competition = .4104034 if district_code == 35031
replace competition = .3684446 if district_code == 35032
replace competition = .3209963 if district_code == 35033
replace competition = .3148645 if district_code == 35034
replace competition = .2522739 if district_code == 35035
replace competition = .389728 if district_code == 35036
replace competition = .2447381 if district_code == 35037
replace competition = .2825407 if district_code == 35038
replace competition = .281238 if district_code == 35039
replace competition = .3501559 if district_code == 35040
replace competition = .3613543 if district_code == 35041
replace competition = .3787188 if district_code == 35042
replace competition = .3449098 if district_code == 35043
replace competition = .3517372 if district_code == 35044
replace competition = .3417515 if district_code == 35045
replace competition = .3272926 if district_code == 35046
replace competition = .3655556 if district_code == 35047
replace competition = .3178435 if district_code == 35048
replace competition = .2784775 if district_code == 35049
replace competition = .2736034 if district_code == 35050
replace competition = .3467306 if district_code == 35051
replace competition = .3639582 if district_code == 35052
replace competition = .2823356 if district_code == 35053
replace competition = .2648674 if district_code == 35054
replace competition = .3785995 if district_code == 35055
replace competition = .372063 if district_code == 35056
replace competition = .3520883 if district_code == 35057
replace competition = .2502924 if district_code == 35058
replace competition = .274622 if district_code == 35059
replace competition = .254658 if district_code == 35060
replace competition = .281478 if district_code == 35061
replace competition = .3638489 if district_code == 35062
replace competition = .2148196 if district_code == 35063
replace competition = .3151003 if district_code == 35064
replace competition = .3947761 if district_code == 35065
replace competition = .2390589 if district_code == 35066
replace competition = .3795213 if district_code == 35067
replace competition = .2680436 if district_code == 35068
replace competition = .4252747 if district_code == 35069
replace competition = .2378599 if district_code == 35070
replace competition = .3085288 if district_code == 35071
replace competition = .3116556 if district_code == 35072
replace competition = .302073 if district_code == 35073
replace competition = .3403153 if district_code == 35074
replace competition = .288044 if district_code == 35075
replace competition = .2719369 if district_code == 35076
replace competition = .3889417 if district_code == 35077
replace competition = .3787881 if district_code == 35078
replace competition = .3230816 if district_code == 35079
replace competition = .3360879 if district_code == 35080
replace competition = .3740905 if district_code == 35081
replace competition = .351243 if district_code == 35082
replace competition = .2790577 if district_code == 35083
replace competition = .324044 if district_code == 35084
replace competition = .4078766 if district_code == 35085
replace competition = .3945665 if district_code == 35086
replace competition = .3792956 if district_code == 35087
replace competition = .2142027 if district_code == 35088
replace competition = .2081704 if district_code == 35089
replace competition = .2430833 if district_code == 35090
replace competition = .2903463 if district_code == 35091
replace competition = .365741 if district_code == 35092
replace competition = .2608916 if district_code == 35093
replace competition = .3778574 if district_code == 35094
replace competition = .3415983 if district_code == 35095
replace competition = .2372467 if district_code == 35096
replace competition = .3329997 if district_code == 35097
replace competition = .3223508 if district_code == 35098
replace competition = .3586551 if district_code == 35099
replace competition = .3092623 if district_code == 35100
replace competition = .3032053 if district_code == 35101
replace competition = .3129015 if district_code == 35102
replace competition = .3189291 if district_code == 35103
replace competition = .2965496 if district_code == 35104
replace competition = .3914787 if district_code == 35105
replace competition = .1850037 if district_code == 35106
replace competition = .289102 if district_code == 35107


*******************************
** INCUMBENT PARTY REELECTED **
*******************************

gen reelected = .

replace reelected = 1 if district_code == 35001
replace reelected = 0 if district_code == 35002
replace reelected = 1 if district_code == 35003
replace reelected = 0 if district_code == 35004
replace reelected = 1 if district_code == 35005
replace reelected = 0 if district_code == 35006
replace reelected = 1 if district_code == 35007
replace reelected = 1 if district_code == 35008
replace reelected = 1 if district_code == 35009
replace reelected = 1 if district_code == 35010
replace reelected = 1 if district_code == 35011
replace reelected = 1 if district_code == 35012
replace reelected = 1 if district_code == 35013
replace reelected = 0 if district_code == 35014
replace reelected = 0 if district_code == 35015
replace reelected = 1 if district_code == 35016
replace reelected = 1 if district_code == 35017
replace reelected = 1 if district_code == 35018
replace reelected = 1 if district_code == 35019
replace reelected = 1 if district_code == 35020
replace reelected = 0 if district_code == 35021
replace reelected = 0 if district_code == 35022
replace reelected = 1 if district_code == 35023
replace reelected = 1 if district_code == 35024
replace reelected = 1 if district_code == 35025
replace reelected = 1 if district_code == 35026
replace reelected = 1 if district_code == 35027
replace reelected = 1 if district_code == 35028
replace reelected = 1 if district_code == 35029
replace reelected = 1 if district_code == 35030
replace reelected = 1 if district_code == 35031
replace reelected = 1 if district_code == 35032
replace reelected = 0 if district_code == 35033
replace reelected = 0 if district_code == 35034
replace reelected = 1 if district_code == 35035
replace reelected = 1 if district_code == 35036
replace reelected = 1 if district_code == 35037
replace reelected = 0 if district_code == 35038
replace reelected = 1 if district_code == 35039
replace reelected = 0 if district_code == 35040
replace reelected = 1 if district_code == 35041
replace reelected = 1 if district_code == 35042
replace reelected = 0 if district_code == 35043
replace reelected = 1 if district_code == 35044
replace reelected = 1 if district_code == 35045
replace reelected = 1 if district_code == 35046
replace reelected = 1 if district_code == 35047
replace reelected = 1 if district_code == 35048
replace reelected = 1 if district_code == 35049
replace reelected = 1 if district_code == 35050
replace reelected = 1 if district_code == 35051
replace reelected = 1 if district_code == 35052
replace reelected = 1 if district_code == 35053
replace reelected = 1 if district_code == 35054
replace reelected = 1 if district_code == 35055
replace reelected = 1 if district_code == 35056
replace reelected = 0 if district_code == 35057
replace reelected = 0 if district_code == 35058
replace reelected = 1 if district_code == 35059
replace reelected = 1 if district_code == 35060
replace reelected = 1 if district_code == 35061
replace reelected = 1 if district_code == 35062
replace reelected = 1 if district_code == 35063
replace reelected = 1 if district_code == 35064
replace reelected = 1 if district_code == 35065
replace reelected = 1 if district_code == 35066
replace reelected = 1 if district_code == 35067
replace reelected = 1 if district_code == 35068
replace reelected = 1 if district_code == 35069
replace reelected = 0 if district_code == 35070
replace reelected = 1 if district_code == 35071
replace reelected = 1 if district_code == 35072
replace reelected = 0 if district_code == 35073
replace reelected = 1 if district_code == 35074
replace reelected = 1 if district_code == 35075
replace reelected = 1 if district_code == 35076
replace reelected = 1 if district_code == 35077
replace reelected = 1 if district_code == 35078
replace reelected = 1 if district_code == 35079
replace reelected = 1 if district_code == 35080
replace reelected = 1 if district_code == 35081
replace reelected = 1 if district_code == 35082
replace reelected = 1 if district_code == 35083
replace reelected = 1 if district_code == 35084
replace reelected = 1 if district_code == 35085
replace reelected = 1 if district_code == 35086
replace reelected = 0 if district_code == 35087
replace reelected = 1 if district_code == 35088
replace reelected = 1 if district_code == 35089
replace reelected = 1 if district_code == 35090
replace reelected = 1 if district_code == 35091
replace reelected = 0 if district_code == 35092
replace reelected = 1 if district_code == 35093
replace reelected = 1 if district_code == 35094
replace reelected = 1 if district_code == 35095
replace reelected = 1 if district_code == 35096
replace reelected = 1 if district_code == 35097
replace reelected = 1 if district_code == 35098
replace reelected = 1 if district_code == 35099
replace reelected = 1 if district_code == 35100
replace reelected = 1 if district_code == 35101
replace reelected = 1 if district_code == 35102
replace reelected = 1 if district_code == 35103
replace reelected = 1 if district_code == 35104
replace reelected = 1 if district_code == 35105
replace reelected = 1 if district_code == 35106
replace reelected = 1 if district_code == 35107

label define reelected 0 "Not reelected" 1 "Reelected"
label values reelected reelected


********************
** RECODE REGIONS **
********************

gen regionON = .

replace regionON = 3 if district_code == 35039
replace regionON = 3 if district_code == 35027
replace regionON = 1 if district_code == 35096
replace regionON = 1 if district_code == 35017
replace regionON = 5 if district_code == 35090
replace regionON = 2 if district_code == 35019
replace regionON = 3 if district_code == 35044
replace regionON = 6 if district_code == 35041
replace regionON = 2 if district_code == 35001
replace regionON = 3 if district_code == 35098
replace regionON = 1 if district_code == 35068
replace regionON = 3 if district_code == 35067
replace regionON = 6 if district_code == 35042
replace regionON = 2 if district_code == 35048
replace regionON = 6 if district_code == 35074
replace regionON = 3 if district_code == 35043
replace regionON = 6 if district_code == 35052
replace regionON = 5 if district_code == 35002
replace regionON = 2 if district_code == 35097
replace regionON = 2 if district_code == 35030
replace regionON = 2 if district_code == 35059
replace regionON = 2 if district_code == 35061
replace regionON = 2 if district_code == 35046
replace regionON = 5 if district_code == 35057
replace regionON = 6 if district_code == 35036
replace regionON = 1 if district_code == 35016
replace regionON = 3 if district_code == 35032
replace regionON = 1 if district_code == 35023
replace regionON = 2 if district_code == 35011
replace regionON = 3 if district_code == 35055
replace regionON = 6 if district_code == 35013
replace regionON = 1 if district_code == 35077
replace regionON = 6 if district_code == 35063
replace regionON = 4 if district_code == 35058
replace regionON = 6 if district_code == 35073
replace regionON = 3 if district_code == 35003
replace regionON = 1 if district_code == 35107
replace regionON = 3 if district_code == 35031
replace regionON = 2 if district_code == 35047
replace regionON = 3 if district_code == 35038
replace regionON = 3 if district_code == 35078
replace regionON = 3 if district_code == 35045
replace regionON = 6 if district_code == 35062
replace regionON = 3 if district_code == 35103
replace regionON = 2 if district_code == 35051
replace regionON = 4 if district_code == 35069
replace regionON = 1 if district_code == 35020
replace regionON = 1 if district_code == 35101
replace regionON = 3 if district_code == 35040
replace regionON = 3 if district_code == 35102
replace regionON = 3 if district_code == 35028
replace regionON = 2 if district_code == 35050
replace regionON = 4 if district_code == 35004
replace regionON = 3 if district_code == 35034
replace regionON = 1 if district_code == 35082
replace regionON = 2 if district_code == 35008
replace regionON = 5 if district_code == 35093
replace regionON = 3 if district_code == 35010
replace regionON = 1 if district_code == 35072
replace regionON = 4 if district_code == 35018
replace regionON = 4 if district_code == 35105
replace regionON = 4 if district_code == 35071
replace regionON = 4 if district_code == 35029
replace regionON = 2 if district_code == 35053
replace regionON = 2 if district_code == 35075
replace regionON = 1 if district_code == 35094
replace regionON = 3 if district_code == 35033
replace regionON = 2 if district_code == 35100
replace regionON = 3 if district_code == 35022
replace regionON = 2 if district_code == 35007
replace regionON = 1 if district_code == 35025
replace regionON = 1 if district_code == 35024
replace regionON = 5 if district_code == 35079
replace regionON = 1 if district_code == 35095
replace regionON = 2 if district_code == 35089
replace regionON = 3 if district_code == 35009
replace regionON = 6 if district_code == 35064
replace regionON = 3 if district_code == 35021
replace regionON = 1 if district_code == 35081
replace regionON = 2 if district_code == 35060
replace regionON = 5 if district_code == 35091
replace regionON = 1 if district_code == 35015
replace regionON = 3 if district_code == 35054
replace regionON = 1 if district_code == 35106
replace regionON = 6 if district_code == 35066
replace regionON = 1 if district_code == 35083
replace regionON = 5 if district_code == 35056
replace regionON = 3 if district_code == 35076
replace regionON = 3 if district_code == 35070
replace regionON = 1 if district_code == 35080
replace regionON = 3 if district_code == 35014
replace regionON = 4 if district_code == 35086
replace regionON = 1 if district_code == 35104
replace regionON = 1 if district_code == 35084
replace regionON = 4 if district_code == 35085
replace regionON = 3 if district_code == 35037
replace regionON = 6 if district_code == 35026
replace regionON = 6 if district_code == 35065
replace regionON = 2 if district_code == 35006
replace regionON = 6 if district_code == 35087
replace regionON = 1 if district_code == 35005
replace regionON = 3 if district_code == 35012
replace regionON = 5 if district_code == 35088
replace regionON = 2 if district_code == 35049
replace regionON = 2 if district_code == 35099
replace regionON = 5 if district_code == 35035

label define regionON 1 "GTA 416" 2 "GTA 905" 3 "Southwestern" 4 "Central" 5 "Northern" 6 "Eastern"
label values regionON regionON


***************************
** INDEPENDENT VARIABLES **
***************************

* Province

gen province = 5

label define province 5 "Ontario"
label values province province

* Age

gen age_cat = age

recode age 1=991 2=992 3=993 4=994 5=995 6=996

label define age 991 "18-24" 992 "25-34" 993 "35-44" 994 "45-54" 995 "55-64" 996 "65+"
label values age age

* Age 55 and over

gen age55 = 1 if age >= 995
replace age55 = 0 if age < 995 & age!=.

label define age55 0 "Less than 55" 1 "55 and over"
label values age55 age55

* Gender

gen male = gender

recode male 2=0

label define gender 0 "Female" 1 "Male"
label values male gender

* Education

gen education = educ

recode education 9=.

label define education 1 "Primary school or less" 2 "Some high school" 3 "High school" 4 "Some community college/CEGEP/Trade school" 5 "Community college/CEGEP/Trade school" 6 "Some university" 7 "University undergraduate degree" 8 "University graduate degree"
label values education education

* University degree

gen univ = 1 if education >= 7
replace univ = 0 if education < 7 & education!=.

label define univ 0 "No university degree" 1 "University degree"
label values univ univ

* Postgraduate degree

gen postgrad = 1 if education == 8
replace postgrad = 0 if education < 8 & education!=.

label define postgrad 0 "No postgraduate degree" 1 "Postgraduate degree"
label values postgrad postgrad

* Postsecondary degree

gen postsec = 1 if education >= 5
replace postsec = 0 if education < 5 & education!=.

label define postsec 0 "No postsecondary" 1 "Postsecondary"
label values postsec postsec

* Immigrant

gen immigrant = q27

recode immigrant 1=0 2=1

label define immigrant 0 "Born in Canada" 1 "Immigrant"
label values immigrant immigrant

* Visible minority

gen minority = q32

recode minority 2=0

label define minority 0 "Not visible minority" 1 "Visible minority"
label values minority minority

* Household income

gen income = hhincome

recode income 2=1 3=1 4=2 5=3 6=4 7=5 8=6 9=7 10=8 11=9 12=9 13=9 14=10 15=11 16=11 17=11 18=12 19=12 20=12 21=13 22=13 23=13 24=13 25=13 26=14 27=14 28=15 29=15 30=15

label define income 1 "Under $10,000" 2 "$10,000-$14,999" 3 "$15,000-$19,999" 4 "$20,000-$24,999" 5 "$25,000-$29,999" 6 "$30,000-$34,999" 7 "$35,000-$39,999" 8 "$40,000-$44,999" 9 "$45,000-$49,999" 9 "$50,000-$54,999" 9 "$45,000-$54,999" 10 "$55,000-$59,999" 11 "$60,000-$69,999" 12 "$70,000-$79,999" 13 "$80,000-$99,999" 14 "$100,000-$149,999" 15 "$150,000 or more"
label values income income

* Income (uniform)

gen income_unif = income

recode income_unif 1/5=991 6/10=992 11/18=993

* Household income (quintile)

xtile income5 = income, nq(5)

* Highest income categories

gen highinc = 1 if income5 == 5
replace highinc = 0 if income5 < 5 & income!=.

label define highinc 0 "Others" 1 "Highest incomes"
label values highinc highinc

* Vote choice

gen vote_choice = vote

recode vote_choice 8=7

replace vote_choice = q1a if vote_choice == .

recode vote_choice 1=351 2=352 3=353 4=354 5=88 6=999 7=-99

label define vote_choice 351 "Progressive Conservative Party of Ontario" 352 "Ontario Liberal Party" 353 "Ontario New Democratic Party" 354 "Green Party of Ontario" 88 "Other" 999 "Spoiled ballot" -99 "Don't know" 
label values vote_choice vote_choice

* Vote status (district)

gen vote_district = 1 if vote_choice == district_outcome
replace vote_district = 0 if vote_choice!=district_outcome & vote_choice!=. 

label define winner 0 "Voted for loser" 1 "Voted for winner"
label values vote_district winner

* Vote status (provincial)

gen vote_whole = 1 if vote_choice == 352
replace vote_whole = 0 if vote_choice!=352 & vote_choice!=.

label values vote_whole winner

* Previous vote

gen voted_prev = q6

gen remember = q7

gen vote_prev = q8

recode vote_prev 6=.

gen prev = 1 if decided == vote_prev & vote_prev!=.
replace prev = 0 if decided!=vote_prev & vote_prev!=.
replace prev = 888 if remember == 2
replace prev = 999 if voted_prev == 2 | voted_prev == 3

label define prev 0 "Changed vote" 1 "Same vote" 888 "Don't remember" 999 "Did not vote/Not sure"
label values prev prev

* How certain are you that this is the party you will support?

gen certain = 5 - q4


*********************************
** DISTRICT-LEVEL EXPECTATIONS **
*********************************

* Forecasts

gen forecast_district = q16

recode forecast_district 1=351 2=352 3=353 4=354 5=88 6=88 7=-99

label define forecast 351 "Progressive Conservative Party of Ontario" 352 "Ontario Liberal Party" 353 "Ontario New Democratic Party" 354 "Green Party of Ontario" 88 "Other" -99 "Don't know"
label values forecast_district forecast

* Identify correct forecasts

gen correct_district = 0 if forecast_district!=district_outcome & forecast_district!=.
replace correct_district = 1 if forecast_district == district_outcome

label define correct 0 "Incorrect" 1 "Correct"
label values correct_district correct


***********************************
** PROVINCIAL-LEVEL EXPECTATIONS **
***********************************

* Forecasts

gen forecast_whole = q17

recode forecast_whole 1=351 2=352 3=353 4=354 5=. 6=88 7=88 8=-99

label values forecast_whole forecast

* Identify correct forecasts

gen correct_whole_d = 0 if forecast_whole!=352 & forecast_whole!=.
replace correct_whole_d = 1 if forecast_whole == 352


*************
** WEIGHTS **
*************

* Sex

gen sex_on2011_w = male

recode sex_on2011_w 0=1 1=2

label define sex_on2011_w 1 "Female" 2 "Male"
label values sex_on2011_w sex_on2011_w

* Age

gen age_on2011_w = age_cat

label define age_on2011_w 1 "18-24" 2 "25-34" 3 "35-44" 4 "45-54" 5 "55-64" 6 "65+"
label values age_on2011_w age_on2011_w

* Education

gen education_on2011_w = education

recode education_on2011_w 2=1 3=2 4=2 5=3 6=3 7=4 8=5

label define education_on2011_w 1 "Below high school" 2 "High school" 3 "Postsecondary below bachelor" 4 "University undergraduate degree" 5 "University graduate degree"
label values education_on2011_w education_on2011_w

* Income

gen income_on2011_w = income

recode income_on2011_w 2/5=1 6/10=2 11/15=3

label define income_on2011_w 1 "$30,000 or less" 2 "$30,000-$59,999" 3 "$60,000 or more"
label values income_on2011_w income_on2011_w


**********
** SAVE **
**********

save "on2011_ipsos_invitation.dta", replace

log close